package cn.tedu.tmall.front.mall.contorller;

import cn.tedu.tamll.common.pojo.CurrentUser;
import cn.tedu.tamll.common.pojo.vo.PageData;
import cn.tedu.tamll.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.OrderAddParam;
import cn.tedu.tmall.front.mall.pojo.vo.OrderDetailVO;
import cn.tedu.tmall.front.mall.pojo.vo.OrderVO;
import cn.tedu.tmall.front.mall.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "购物车模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private IOrderService orderService;


    @PostMapping("create")
    @ApiOperation("创建订单")
    public JsonResult createOrder(
            @AuthenticationPrincipal @ApiIgnore CurrentUser user,
            @RequestBody OrderAddParam orderAddParam)
    {
        String orderCode = orderService.createOrder(user, orderAddParam);
        return JsonResult.ok(orderCode);
    }

    /**
     *
     * @param user
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("list")
    @ApiOperation("订单列表")
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                           @RequestParam Integer pageSize,
                           @RequestParam Integer pageNum) {
        PageData<OrderVO> orders = orderService.list(user,pageSize,pageNum);
        return JsonResult.ok(orders);
    }

    @GetMapping("detail")
    @ApiOperation("订单详情")
    public JsonResult detail(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                           @RequestParam String orderNo) {
        OrderDetailVO detail = orderService.detail(user,orderNo);
        return JsonResult.ok(detail);
    }
}
